﻿
// <copyright file="CompositeControllableComponent.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The composite controllable component.
// </summary>

namespace SubhadraSolutions.Sharp.Utils
{
    using System;
    using System.Collections.Generic;

    using SubhadraSolutions.Sharp.Utils.Contracts;

    /// <summary>
    /// The composite controllable component.
    /// </summary>
    public sealed class CompositeControllableComponent : AbstractInitializableAndDisposable, ICompositeControllable
    {
        #region Fields

        /// <summary>
        /// The controller.
        /// </summary>
        private readonly GenericComponentController _controller;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="CompositeControllableComponent"/> class.
        /// </summary>
        /// <param name="components">
        /// The components.
        /// </param>
        public CompositeControllableComponent(IEnumerable<IControllable> components)
            : this()
        {
            foreach (IControllable component in components)
            {
                this.Controllables.Add(component);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompositeControllableComponent"/> class.
        /// </summary>
        public CompositeControllableComponent()
        {
            this.Controllables = new List<IControllable>();
            this._controller = new GenericComponentController(this.start, this.stop, this.pause, this.resume);
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets or sets the controllables.
        /// </summary>
        public IList<IControllable> Controllables { get; set; }

        /// <summary>
        /// Gets the controller.
        /// </summary>
        public IComponentController Controller
        {
            get
            {
                return this._controller;
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name { get; set; }

        #endregion Public Properties

        #region Methods

        /// <summary>
        /// The dispose.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            foreach (IControllable controllable in this.Controllables)
            {
                var disposable = controllable as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
        }

        /// <summary>
        /// The initialize protected.
        /// </summary>
        protected override void InitializeProtected()
        {
            foreach (IControllable controllable in this.Controllables)
            {
                var initializable = controllable as IInitializable;
                if (initializable != null && !initializable.IsInitialized)
                {
                    initializable.Initialize();
                }
            }
        }

        /// <summary>
        /// The pause.
        /// </summary>
        private void pause()
        {
            foreach (IControllable controllable in this.Controllables)
            {
                if (controllable.Controller != null)
                {
                    controllable.Controller.Pause();
                }
            }
        }

        /// <summary>
        /// The resume.
        /// </summary>
        private void resume()
        {
            foreach (IControllable controllable in this.Controllables)
            {
                if (controllable.Controller != null)
                {
                    controllable.Controller.Resume();
                }
            }
        }

        /// <summary>
        /// The start.
        /// </summary>
        private void start()
        {
            foreach (IControllable controllable in this.Controllables)
            {
                if (controllable.Controller != null)
                {
                    controllable.Controller.Start();
                }
            }
        }

        /// <summary>
        /// The stop.
        /// </summary>
        private void stop()
        {
            foreach (IControllable controllable in this.Controllables)
            {
                if (controllable.Controller != null)
                {
                    controllable.Controller.Stop();
                }
            }
        }

        #endregion Methods
    }
}